<?php

namespace app\common\lib;

Class Param{

    /**
     * @param $params
     * @param null $request
     * @param bool $suffix
     * @return array
     * 接收参数封装
     */
    public static function getMore($params, $request=null, bool $suffix = false): array
    {
        return self::extracted($request, $params, $suffix);
    }

    /**
     * @param $params
     * @param null $request
     * @param bool $suffix
     * @return array
     * 接收参数封装
     */
    public static function postMore($params, $request = null, bool $suffix = false): array
    {
        return self::extracted($request, $params, $suffix);
    }

    /**
     * @param $request
     * @param $params
     * @param bool $suffix
     * @return array
     */
    public static function extracted($request, $params, bool $suffix): array
    {
        if ($request === null) $request = app('request');
        $p = [];
        $i = 0;
        foreach ($params as $param) {
            if (!is_array($param)) {
                $p[$suffix ? $i++ : $param] = $request->param($param);
            } else {
                if (!isset($param[1])) $param[1] = null;
                if (!isset($param[2])) $param[2] = '';
                $name = is_array($param[1]) ? $param[0] . '/a' : $param[0];
                $p[$suffix ? $i++ : ($param[3] ?? $param[0])] = $request->param($name, $param[1], $param[2]);
            }
        }
        return $p;
    }
}